package org.dftproject.genesis.ui.pages.home;

import java.util.List;

import org.dftproject.genesis.core.collections.Collection;
import org.dftproject.genesis.core.collections.CollectionManager;
import org.dftproject.genesis.core.collections.ICollectionManagerListener;
import org.dftproject.genesis.ui.Activator;
import org.dftproject.genesis.ui.editors.BrowserEditor;
import org.dftproject.genesis.ui.editors.BrowserEditorInput;
import org.dftproject.genesis.ui.figures.CollectionFigure;
import org.dftproject.genesis.ui.figures.FigureCanvasEx;
import org.dftproject.genesis.ui.figures.HyperlinkFigure;
import org.dftproject.genesis.ui.figures.IHyperlinkListener;
import org.dftproject.genesis.ui.figures.LinedContainer;
import org.dftproject.genesis.ui.figures.PageContainer;
import org.dftproject.genesis.ui.figures.UniformSizeGroup;
import org.dftproject.genesis.ui.pages.AbstractPagePart;
import org.dftproject.genesis.ui.pages.AddCollectionPage;
import org.dftproject.genesis.ui.pages.AddCollectionPageInput;
import org.dftproject.genesis.ui.pages.AddOnlineCollectionPage;
import org.dftproject.genesis.ui.pages.AddOnlineCollectionPageInput;
import org.dftproject.genesis.ui.pages.IPageInput;
import org.dftproject.genesis.ui.pages.IPageSite;
import org.dftproject.genesis.ui.pages.PageInitException;
import org.dftproject.genesis.ui.pages.PageManager;
import org.dftproject.genesis.ui.pages.PageUtils;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

public class HomePage extends AbstractPagePart {

	public static final String ID = HomePage.class.getName();
	
	private ICollectionManagerListener collectionManagerListener;
	
	private FigureCanvas canvas;
	private Figure topContainer;
	
	public void init(IPageSite site, IPageInput input) throws PageInitException {
		if (!(input instanceof HomePageInput))
			throw new PageInitException("input must be a " + HomePageInput.class.getName());
		this.setSite(site);
		this.setInput(input);
	}

	@Override
	public void createPartControl(Composite parent) {
		collectionManagerListener = new ICollectionManagerListener() {

			public void collectionAdded(Collection collection) {
				updateCollections();
			}
			
			public void collectionUpdated(Collection collection) {
				updateCollections();
			}
			
			public void collectionRemoved(Collection collection) {
				updateCollections();
			}
			
		};
		CollectionManager.getDefault().addListener(collectionManagerListener);
		
		createContents(parent);
	}
	
	protected void createContents(Composite parent) {
		// Create the main Draw2D canvas

		canvas = new FigureCanvasEx(parent);

		// Create the root figure

		topContainer = new PageContainer();
		canvas.getViewport().setContentsTracksWidth(true);
		canvas.setContents(topContainer);
		
		if (CollectionManager.getDefault().getCollections().isEmpty())
			createWelcome(topContainer);
		else
			createHome(topContainer);
	}
	
	protected void createWelcome(IFigure parent) {		
		parent.add(PageUtils.createH1Label("Let's Get Started!"));
		
		parent.add(PageUtils.createFlowedText("Genesis can help you learn more about your ancestors, but first you need somewhere to begin. Here are a few ways to get started:"));
		
		IFigure links = new Figure();
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(false);
		links.setLayoutManager(layout);
		links.setBorder(new MarginBorder(10, 10, 30, 0));
		parent.add(links);
		
		HyperlinkFigure openLink = new HyperlinkFigure("Open a file", Activator.getImageDescriptor("icons/folder_page.png").createImage());
		openLink.addHyperlinkListener(new IHyperlinkListener() {

			public void linkActivated(Object data, int stateMask) {
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				IWorkbenchPage workbenchPage = window.getActivePage();
				PageManager.getDefault().openPage(workbenchPage, new AddCollectionPageInput(), AddCollectionPage.ID, true, (stateMask & SWT.CTRL) != 0);
			}
			
		});
		links.add(openLink);
		
		HyperlinkFigure serviceLink = new HyperlinkFigure("Connect to a Web site", Activator.getImageDescriptor("icons/world.png").createImage());
		serviceLink.addHyperlinkListener(new IHyperlinkListener() {

			public void linkActivated(Object data, int stateMask) {
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				IWorkbenchPage workbenchPage = window.getActivePage();
				PageManager.getDefault().openPage(workbenchPage, new AddOnlineCollectionPageInput(), AddOnlineCollectionPage.ID, true, (stateMask & SWT.CTRL) != 0);
			}
			
		});
		links.add(serviceLink);
		
		HyperlinkFigure searchLink = new HyperlinkFigure("Search on the Internet", Activator.getImageDescriptor("icons/magnifier.png").createImage());
		searchLink.addHyperlinkListener(new IHyperlinkListener() {

			public void linkActivated(Object data, int stateMask) {
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				IWorkbenchPage workbenchPage = window.getActivePage();
				try {
					workbenchPage.openEditor(new BrowserEditorInput("http://www.familytreeseeker.com/"), BrowserEditor.ID, true, IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
				} catch (PartInitException e) {
					MessageDialog.openError(window.getShell(), "Error", "Error opening browser: " + e.getMessage());
				}
			}
			
		});
		links.add(searchLink);
		
		createVolunteer(parent);
	}

	protected void createHome(IFigure parent) {
		parent.add(PageUtils.createH1Label("Let's Get Started!"));
		
		IFigure links = new Figure();
		ToolbarLayout layout = new ToolbarLayout(true);
		layout.setSpacing(30);
		layout.setStretchMinorAxis(false);
		links.setLayoutManager(layout);
		links.setBorder(new MarginBorder(10, 0, 0, 0));
		parent.add(links);
		
		HyperlinkFigure openLink = new HyperlinkFigure("Open a file", Activator.getImageDescriptor("icons/folder_page.png").createImage());
		openLink.addHyperlinkListener(new IHyperlinkListener() {

			public void linkActivated(Object data, int stateMask) {
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				IWorkbenchPage workbenchPage = window.getActivePage();
				PageManager.getDefault().openPage(workbenchPage, new AddCollectionPageInput(), AddCollectionPage.ID, true, (stateMask & SWT.CTRL) != 0);
			}
			
		});
		links.add(openLink);
		
		HyperlinkFigure serviceLink = new HyperlinkFigure("Connect to a Web site", Activator.getImageDescriptor("icons/world.png").createImage());
		serviceLink.addHyperlinkListener(new IHyperlinkListener() {

			public void linkActivated(Object data, int stateMask) {
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				IWorkbenchPage workbenchPage = window.getActivePage();
				PageManager.getDefault().openPage(workbenchPage, new AddOnlineCollectionPageInput(), AddOnlineCollectionPage.ID, true, (stateMask & SWT.CTRL) != 0);
			}
			
		});
		links.add(serviceLink);
		
		HyperlinkFigure searchHyperlink = new HyperlinkFigure("Search on the Internet", Activator.getImageDescriptor("icons/magnifier.png").createImage());
		searchHyperlink.addHyperlinkListener(new IHyperlinkListener() {

			public void linkActivated(Object data, int stateMask) {
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				IWorkbenchPage workbenchPage = window.getActivePage();
				try {
					workbenchPage.openEditor(new BrowserEditorInput("http://www.familytreeseeker.com/"), BrowserEditor.ID, true, IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
				} catch (PartInitException e) {
					MessageDialog.openError(window.getShell(), "Error", "Error opening browser: " + e.getMessage());
				}
			}
			
		});
		links.add(searchHyperlink);
		
		LinedContainer collectionsList = new LinedContainer();
		collectionsList.setBorder(new MarginBorder(30, 10, 30, 0));
		parent.add(collectionsList);
		
		UniformSizeGroup linkGroup = new UniformSizeGroup();
		for (Collection collection : CollectionManager.getDefault().getRecentCollections(0)) {
			CollectionFigure figure = new CollectionFigure(collection, linkGroup);
			collectionsList.add(figure);
		}
		
		createVolunteer(parent);
	}

	protected void createVolunteer(IFigure parent) {
		parent.add(PageUtils.createH1Label("Help Others"));
		
		parent.add(PageUtils.createFlowedText("You might also be interested in helping others learn more about their ancestors:"));
		
		IFigure links = new Figure();
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(false);
		links.setLayoutManager(layout);
		links.setBorder(new MarginBorder(10, 10, 10, 0));
		parent.add(links);
		
		HyperlinkFigure indexingLink = new HyperlinkFigure("FamilySearch Indexing", Activator.getImageDescriptor("icons/report.png").createImage());
		indexingLink.addHyperlinkListener(new IHyperlinkListener() {

			public void linkActivated(Object data, int stateMask) {
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				IWorkbenchPage workbenchPage = window.getActivePage();
				try {
					workbenchPage.openEditor(new BrowserEditorInput("http://www.familysearchindexing.org/"), BrowserEditor.ID, true, IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
				} catch (PartInitException e) {
					MessageDialog.openError(window.getShell(), "Error", "Error opening browser: " + e.getMessage());
				}
			}
			
		});
		links.add(indexingLink);
	}

	protected void updateCollections() {
		Display.getDefault().asyncExec(new Runnable() {

			public void run() {
				if (canvas == null || canvas.isDisposed())
					return;
				
				List<Collection> collections = CollectionManager.getDefault().getRecentCollections(1);
				if (collections.isEmpty()) {
					topContainer.removeAll();
					createWelcome(topContainer);
				} else {
					topContainer.removeAll();
					createHome(topContainer);
				}
			}
			
		});
	}

	@Override
	public void dispose() {
		CollectionManager.getDefault().removeListener(collectionManagerListener);
	}

	@Override
	public void setFocus() {
		canvas.setFocus();
	}

}
